--[[
Copyright (c) 2012 Hello!Game

Permission is hereby granted, free of charge, to any person obtaining a copy
of newinst software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and newinst permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
]]

local floor = math.floor;
local random = math.random;
local sqrt = math.sqrt;

local Object = getClass("Object");
local Noise = getClass("Object.Noise");

local Diamond = class("Diamond",Object,
{
	map_dimension = 128,
	unit_size = 1,
	roughness = 10,				-- 起伏度
	
	consecutive_h = false,		-- 水平连续
	consecutive_v = false,		-- 垂直连续
});

local function normalize(value)
	if value > 1 then
		value = 1;
	elseif value < 0 then
		value = 0;
	end
	return value;
end

function Diamond:initialize(size)
	self.map_dimension = size or Diamond.map_dimension;

	self.displace = function (num)
		local max = num/(self.map_dimension + self.map_dimension) * self.roughness;       
		return (random()- 0.5) * max;  
	end
	self.startDisplacement = function (noise, co)
		local md = self.map_dimension
		local x = md;
		local y = md;
		
		local tl, t, tr;
		local l, ct, r;
		local bl, b, br;
		
		local map = noise.noise[0];
		
		-- 初始化数据
		for i=0, md do
			map[i] = {};
			for j=0, md do
				map[i][j] = 0;
			end
		end
		
		-- top left
		map[0][0] = random();
		tl = map[0][0];
		
		-- bottom left
		if self.consecutive_v then
			map[0][md] = tl;
		else
			map[0][md] = random();
		end
		bl = map[0][md];
		
		-- top right
		if self.consecutive_h then
			map[md][0] = tl;
		else
			map[md][0] = random();
		end
		tr = map[md][0];
		
		-- bottom right
		if self.consecutive_h then
			map[md][md] = bl;
		else
			map[md][md] = random();
		end
		br = map[md][md]
		
		-- Center
		map[md/2][md/2] = (tl+tr+bl+br)/4;
		map[md/2][md/2] = normalize(map[md/2][md/2]);
		ct = map[md/2][md/2];
		
		map[md/2][md] = (bl+br+ct)/3;
		map[md/2][0] = (tl+tr+ct)/3;
		map[md][md/2] = (tr+br+ct)/3;
		map[0][md/2] = (tl+bl+ct)/3;
		
		-- Call displacment 
		self.midpointDisplacment(noise, md, co);
	end
	self.midpointDisplacment = function (noise, dimension, co)
		local md = self.map_dimension;
		local nd = dimension/2;
		local map = noise.noise[0];
			
		local tl, t, tr;
		local l, ct, r;
		local bl, b, br;
		
		if nd>self.unit_size then
			for i=nd,md,nd do
				for j=nd,md,nd do
					x = i - (nd / 2);
					y = j - (nd / 2);
					
					tl = map[i - nd][j - nd]; 
					tr = map[i][j - nd];
					bl = map[i - nd][j];
					br = map[i][j];
					
					-- Center				
					map[x][y] = (tl + tr + bl + br) / 4 + self.displace(dimension);
					map[x][y] = normalize(map[x][y]);
					ct = map[x][y];	
					
					-- Top
					if j - (nd * 2) + (nd / 2) > 0 then
						map[x][j - nd] = (tl + tr + ct + map[x][j - dimension + (nd / 2)]) / 4 + self.displace(dimension);
					else
						map[x][j - nd] = (tl + tr + ct) / 3+ self.displace(dimension);
					end
					map[x][j - nd] = normalize(map[x][j - nd]);
			
					-- Bottom
					if self.consecutive_v and j==md then
						map[x][j] = map[x][0]
					else
						if j + (nd / 2) < md then
							map[x][j] = (bl + br + ct + map[x][j + (nd / 2)]) / 4+ self.displace(dimension);
						else
							if  self.consecutive_v then
								map[x][j] = (bl + br + ct + map[x][j + (nd / 2)-md]) / 4+ self.displace(dimension);
							else
								map[x][j] = (bl + br + ct) / 3+ self.displace(dimension);
							end
						end
						map[x][j] = normalize(map[x][j]);
					end

					-- Left
					if i - (nd * 2) + (nd / 2) > 0 then
						map[i - nd][y] = (tl + bl + ct + map[i - dimension + (nd / 2)][y]) / 4 + self.displace(dimension);
					else
						map[i - nd][y] = (tl + bl + ct) / 3+ self.displace(dimension);
					end
					map[i - nd][y] = normalize(map[i - nd][y]);
					
					-- Right
					if self.consecutive_h and i==md then
						map[i][y] = map[0][y]
					else
						if i + (nd / 2) < md then
							map[i][y] = (tr + br + ct + map[i + (nd / 2)][y]) / 4+ self.displace(dimension);
						else
							if self.consecutive_h then
								map[i][y] = (tr + br + ct + map[i + (nd / 2)-md][y]) / 4+ self.displace(dimension);
							else
								map[i][y] = (tr + br + ct) / 3+ self.displace(dimension);
							end
						end
						map[i][y] = normalize(map[i][y]);
					end
				end
				if co then
					coroutine.yield(noise);
				end
			end
			self.midpointDisplacment(noise, nd, co);
		end
	end
end

function Diamond:Generate(noise, co)
	co = co or false;

	-- 检查数据
	if type(self.roughness)~="number" or self.roughness<0 then
		self.roughness = 10;
	end
	
	if type(self.map_dimension)~="number" or self.map_dimension<0 then
		self.map_dimension = 128;
	else
		local fix_dimension = 2;
		while fix_dimension < self.map_dimension do
			fix_dimension = fix_dimension * 2;
		end
		self.map_dimension = fix_dimension;
	end

	if type(self.unit_size)~="number" or self.unit_size<0 then
		self.unit_size = 1;
	end
	
	if noise then
		noise:setSize(self.map_dimension,self.map_dimension);
	else
		noise = Noise(self.map_dimension, self.map_dimension);
	end
	self.startDisplacement(noise, co);
	return noise;
end

function Diamond:CreateCo(noise)
	local co = coroutine.create(function()
		return self:Generate(noise, true);
	end);
	return co;
end

return Diamond;
